home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14475 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.9 KB  |  118 lines

  1. Newsgroups: comp.lang.c
  2. Path: new-news.sprintlink.net!eskimo!news
  3. From: mag@eskimo.com (mAg)
  4. Subject: Re: Can anyone help a newbie out ?
  5. X-Nntp-Posting-Host: tia1.eskimo.com
  6. Message-ID: <Dpw39z.1Gy@eskimo.com>
  7. Sender: news@eskimo.com (News User Id)
  8. Organization: *.*
  9. X-Newsreader: WinVN 0.93.10
  10. References: <4kkf3r$5b0@darwin.nbnet.nb.ca>
  11. Date: Mon, 15 Apr 1996 05:58:47 GMT
  12.  
  13. In article <4kkf3r$5b0@darwin.nbnet.nb.ca> (Fri, 12 Apr 1996 02:38:04 GMT), 
  14. lewwid@brunswickmicro.nb.ca says :
  15. >
  16. >I started reading a book on C, and i am stuck on one exercise, and i
  17. >know it's really simple, but i can't do it :(  
  18. >
  19. >Problem:Write a function that accepts two strings.  Count the number
  20. >of characters in each string, and return the pointer to the longer
  21. >string.
  22. >
  23. >This is what my lamer brain had to say ...
  24. >
  25. >#include <stdio.h>
  26. >
  27. >char *ptr, *ptr1;
  28.  
  29. These are just pointers. They are pointing to nowhere. You can either say
  30.  
  31. char ptr[512],ptr[512]; /* I've chosen 512 arbitrarily */
  32.  
  33. or
  34.  
  35. keep them as 
  36. char *ptr, *ptr1;
  37.  
  38. #include "string.h" at the start of the program and
  39.  
  40. say 
  41.  
  42. if (!(ptr = malloc(512 * sizeof(char))) || !(ptr1 == malloc(512 * sizeof(char))))
  43.     {
  44.     printf("Out of memory\n");
  45.     exit(1);
  46.     }
  47. >
  48. >char checksize(char x[], char y[]);
  49.  
  50. char *checksize(char x[], char y[]);
  51.  
  52. >
  53. >main()
  54. >{
  55. >        /* Prompt for input */
  56. >
  57. >        puts("Please enter in the first string :");
  58. >        gets(ptr);
  59. >        puts("Now the second :");
  60. >        gets(ptr1);
  61. >
  62. it is better to use fgets because you can specody the size and then the user cannot 
  63. crash your program by typing a Loooooong string longer than your array size.
  64.  
  65. >        /* Print the longest pointer, and call checksize */
  66. >
  67. >        printf("The char array that was longer is : %s", checksize(ptr,
  68. >ptr1));
  69. >
  70. >        return 0;
  71. >}
  72. >/* Checksize .. checks to see which array of char is longer, and
  73. >returns the longest of the 2 */
  74. >char checksize(char x[], char y[])
  75.  
  76.  
  77. char *checksize(char x[], char y[])
  78. >{
  79. >        int count, int total = 0, int total1 = 0;
  80. >
  81. >        while (*x != NULL)
  82. >                total += 1
  83. >
  84. >        while (*y != NULL)
  85. >                total1 += 1
  86. >        
  87. >        if (total < total2)
  88. >                return *y;
  89.  
  90.                  return(y);
  91.  
  92. >        if (total > total2)
  93. >                return *x;
  94.  
  95.                  return(x);
  96. >}
  97. >
  98. >
  99. >        Yes i know, this is messy and very bad, but i am just starting out :)
  100. >
  101. >        If someone could email me with any help, and good pointers, that would
  102. >be greatly appreciated ..
  103. >
  104. >        lewwid@brunswickmicro.nb.ca
  105. >
  106.  
  107. I guess everything else looks OK.
  108.  
  109. -- 
  110. /* --------------------------------------------------------
  111.                       MAG@ESKIMO.COM
  112. http://www.eskimo.com/~mag/index.html
  113. ***********************************************************
  114. To understand recursion one must first understand recursion
  115. ***********************************************************
  116. -------------------------------------------------------- */
  117.  
  118.